<!DOCTYPE html>
<html>

<head lang="en">
    <title>找攻略</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/plugins/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/plugins/jquery/jquery.min.js"></script>
    <script src="/js/plugins/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategy.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <script>
        $(function () {
            //地区id
            var placeId;

            //获取游记攻略推荐
            $.get('/travels/commends', {type: 3, pageSize: 1}, function (data) {
                //判断data对象集合是否存在数据
                if (data.list.length > 0) {
                    //data集合中存在数据就只显示一条
                    $(".commend").renderValues(data.list[0]);
                }
                //给a链接绑定事件 (渲染完后才可以绑定)
                $(".commend a[data-url]").click(function () {
                    var url = $(this).data("url");
                    var id = $(this).data("tid");
                    window.location.href = url + id;
                })
            })
            //获取推荐攻略
            $.get('/strategies', {state: 1, pageSize: 0}, function (data) {
                $(".strategyCommend").renderValues(data);

                //给a链接绑定事件 (渲染完后才可以绑定)
                $(".strategyCommend a[data-url]").click(function () {
                    var url = $(this).data("url");
                    var id = $(this).data("sid");
                    window.location.href = url + id;
                })
            })
            //获取推荐地区
            $.get('/regions', {state: 1}, function (data) {
                var html = "";
                var contentHtml = "";
                $.each(data, function (index, ele) {
                    html +='<li class="nav-item"><a data-id="'+ele.id+'"class="nav-link" id="pills-'
                        +ele.id+'-tab" data-toggle="pill" href="#pills-'+ele.id+'">'
                        +ele.name+'</a> </li>';
                    contentHtml +='<div class="tab-pane fade" id="pills-'+ele.id+'"></div>';

                })
                //设置导航头
                $("#pills-tab").append(html);
                //内容
                $("#pills-tabContent").append(contentHtml);

                //绑定点击事件
                $(".nav-link").click(function () {
                    //获取地区的id
                    var id = $(this).data("id");
                    //设置到全局变量中
                    placeId = id;
                    //重置分页数据
                    currentPage.val(2);//当前页
                    pages=2;//总页数
                    //获取html
                    $.ajax({
                        dataType: 'html',
                        data: {
                            pageSize: pageSize
                        },
                        url:'/regions/'+id+'/strategies',
                        success: function (data) {
                            //获取地区对应的div内容
                            $("#pills-" + id).html(data);
                        }
                    })
                })
            })
            //每页显示数量
            var pageSize = $("#pageSize").val();

            //当前页input框
            var currentPage = $("#currentPage");
            //总页数
            var pages = 2;
            //用于存储攻略列表的数据
            //var strategyDate = [];

            //分页查询的方法
            function query() {
                //当前页的值
                var currentPageValue = currentPage.val();
                //获取所有游记的分页数据
                $.ajax({
                    dataType: 'json',
                    data: {currentPage: currentPageValue, pageSize: pageSize},
                    url: '/regions' + placeId + '/strategies',
                    success: function (data) {
                        var content = "";
                        $.each(data.list, function (index, ele) {
                            content += '<div class="col-6 mb"><a href="../strategyCatalogs.html?id="' + ele.id + '>'
                                + '<img class="float-left " src=' + ele.coverUrl + '">'
                                + '<div class="classify-text"><span>' + ele.title + '</span>'
                                + '<p>0人收藏</p> </div> </a> </div>';

                        })

                        //把获取到的攻略数据拼接到div内容
                        $(".classify").append(content);

                        //当前页加一(确保获取到第二页数据后currentPage再加一)
                        currentPage.val(parseInt(currentPageValue) + 1);
                        //设置总页数的值
                        pages = data.pages;

                        //给a链接绑定事件 (渲染完后才可以绑定)

                    }
                })


            }


            //滚动事件
            $(window).scroll(function () {
                //document(html)卷上去的高度+窗口的高度>+document(html)的整个高度
                if ($(document).scrollTop() + $(window).height() >= $(document).height()) {
                    //当前页的值
                    var currentPageValue = currentPage.val();
                    //判断currentPageValue当前页是否大于pages总页数
                    if (currentPageValue <= pages) {
                        query();

                    } else {
                        //提示用户已经到底了

                        $(document).dialog({
                            type: 'notice',
                            infoText: '已经到底了',
                            autoClose: 1000
                        });

                    }
                }
            })

        })
    </script>
</head>

<body>

<input type="hidden" name="currentPage" id="currentPage" value="2">
<input type="hidden" name="pageSize" id="pageSize" value="8">
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="index.html">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <input class="form-control searchBtn" placeholder="找攻略">
            </div>
        </div>
    </div>
</div>

<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab" role="tablist">
    <li class="nav-item">
        <a class="nav-link active show" id="pills-top-tab" data-toggle="pill" href="#pills-top">
            <span style="font-weight: bold;">推荐</span>
        </a>
    </li>


</ul>
<!--显示内容-->
<div class="tab-content" id="pills-tabContent">
    <div class="tab-pane fade active show" id="pills-top">
        <div class="container commend">

            <a href="javascript:;" data-url="travelContent.html?id=" render-attr="data-tid=travel_id">
                <img render-src="coverUrl">
                <p render-html="title"></p>
            </a>

        </div>
        <hr>
        <div class="container strategyCommend">
            <h6>当季推荐</h6>
            <div class="row hot" render-loop="list">
                <div class="col-4">
                    <a href="javascript:;" data-url="strategyCatalogs.html?strategyId=" render-attr="data-sid=list.id">
                        <img render-src="list.coverUrl">
                        <p render-html="list.title"></p>
                    </a>
                </div>
            </div>
        </div>

    </div>
</div>

</body>

</html>